import { _decorator, Component, Node, UI, Label, Game, clamp01, Sprite, clamp, Tween, tween, Rect, rect, SpriteFrame, size, UITransform, Size, Vec3, v3, view, ImageAsset, Texture2D, v4, Button } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { PlatformType, SDKSystem } from '../../Init/SystemSDK/SDKSystem';
import Tools from '../../Init/Tools/Tools';
const { ccclass, property } = _decorator;

@ccclass('WinUI')
export class WinUI extends BasicUI {

    @property(Node)
    bgMask: Node = null;
    // 延迟关闭-显示奖励动画
    delayCloseAnim = { time: 0 };

    public show() {
        super.show();
        // 退出 Log
        EventManager.emit(EventTypes.SDKEvents.ExitStage, GlobalEnum.ExitLvState.Win);

        UISystem.hideUI(UIEnum.LevelInfoUI);
        UISystem.showUI(UIEnum.PlayerAssetsUI);

        Tween.stopAllByTarget(this.delayCloseAnim);

        AudioSystem.playEffect(AudioEnum.Win);

        this.initInfo();
        this.initBtns();
    }

    public hide(d?: any): void {
        super.hide(d);
        Tween.stopAllByTarget(this.delayCloseAnim);
    }

    protected update(dt: number): void {
    }

    //#region --------奖励/信息--------- 
    @property(Label)
    getGold: Label = null;

    // 是否已获得视频奖励
    isVideoAward = false;
    // 当前关卡
    curLv = 0;

    award = {
        gold: 0,
    }

    private initInfo() {
        this.isVideoAward = false;
        this.curLv = GlobalTmpData.CurLv;


        this.setVideoBtnEnable(true);
        // 设置奖励
        this.award.gold = GlobalTmpData.AnimalData.curGetGold;

        // 默认领取
        const userAsset = StorageSystem.getData().userAssets;
        userAsset.asset += this.award.gold;
        StorageSystem.saveData();

        this.showAwardsInfo();
    }

    // 展示奖励信息
    private showAwardsInfo() {
        this.getGold.string = this.award.gold.toFixed(0);

    }

    // 是否已经获得奖励
    private setVideoBtnEnable(enable: boolean) {
        this.isVideoAward = !enable;
        //
    }

    // 保存奖励->关闭当前界面
    private showDelayCloseAnim(isAnim, cb?) {
        let delayTime = 0;

        if (isAnim) {
            EventManager.emit(EventTypes.UIEvents.ShowTouchMask);

            tween(this.delayCloseAnim).delay(delayTime).call(() => {
                //
                EventManager.emit(EventTypes.UIEvents.HideTouchMask);
                cb && cb();
            }).start();
        }
    }

    //  #endregion

    //#region --------功能

    closeWinUI(cb) {
        // Loading 过度
        UISystem.showUI(UIEnum.LoadingUI, {
            enterCb: () => {
                // 关闭当前界面
                UISystem.hideUI(UIEnum.WinUI);
                // 手动清除关卡场景
                this.emit(EventTypes.GameEvents.SetLevelManagerEnable, false);
                cb && cb();
            }
        })
    }
    // 返回首页
    backToHome() {
        this.closeWinUI(() => {
            this.emit(EventTypes.GameEvents.BackHome, true);
        })
    }
    // 继续游戏
    continueGame() {
        this.closeWinUI(() => {
            // 扣除次数
            const userAssets = StorageSystem.getData().userAssets;
            userAssets.daliyFreeLvCount--;
            StorageSystem.saveData();

            EventManager.emit(EventTypes.GameEvents.GameStart);
        })
    }
    //#endregion

    //#region --------按钮--------- 

    @property(Node)
    nextBtn: Node = null;

    @property(Node)
    homeBtn: Node = null;

    protected initBtns() {
        // 按钮状态

    }

    // 进入下一关
    protected onNextLevel() {
        this.continueGame();
    }

    protected onBackHome() {
        this.backToHome();
    }

    //视频领取
    protected onVideoClick() {
        if (this.isVideoAward) return;


        this.emit(EventTypes.SDKEvents.ShowVideo, () => {

            // 奖励

            // 存档


            // 更新信息
            this.setVideoBtnEnable(false);
        }, GlobalEnum.VideoIdx.LvPass);
    }
    //直接关闭
    protected onCloseClick() {
        this.backToHome();
    }

    //分享录屏按钮
    protected onShareClick() {
        switch (SDKSystem._curPlatform) {
            case PlatformType.WXMiniGame:
                this.onTranspondBtnClick();
                break;
            case PlatformType.TTMiniGame:
                this.onShareRecord();
                break;
            default:
                break;
        }
    }

    // 分享录屏-抖音
    protected onShareRecord() {
        EventManager.emit(EventTypes.SDKEvents.ShareRecord, () => {
        });
    }

    // 转发-wx share
    protected onTranspondBtnClick() {
        EventManager.emit(EventTypes.SDKEvents.Share, { title: '终于过关啦!', useUrl: true, urlType: GlobalEnum.SharePosition.WinPage })
        EventManager.emit(EventTypes.SDKEvents.ShareReport, '胜利界面');

    }

    // #endregion

}


